package com.boe.web.action;

import com.boe.domain.Category;
import com.boe.service.CategoryService;
import com.boe.service.impl.CategoryServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "CategoryServlet", urlPatterns = "/CategoryServlet")
public class CategoryServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String methodName = request.getParameter("method");
        if(methodName.equals("findAll")){
            findAll(request,response);
        }else if(methodName.equals("saveUI")){
            saveUI(request,response);
        }else if(methodName.equals("save")){
            save(request,response);
        }else if(methodName.equals("edit")){
            edit(request,response);
        }else if(methodName.equals("update")){
            update(request,response);
        }else if(methodName.equals("delete")){
            delete(request,response);
        }
    }

    /**
     * 分类删除功能
     * @param request
     * @param response
     */
    private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取参数
        Integer cid = Integer.parseInt(request.getParameter("cid"));
        //转发业务层处理
        CategoryService categoryService= new CategoryServiceImpl();
        categoryService.delete(cid);

        //转发页面
        response.sendRedirect(request.getContextPath()+"CategoryServlet?method=findAll");


    }

    /**
     * 分类修改后提交按钮
     * @param request
     * @param response
     */
    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取参数
        Integer cid = Integer.parseInt(request.getParameter("cid"));
        String cname = request.getParameter("cname");
        String cdesc = request.getParameter("cdesc");
        //封装参数
        Category category =new Category();
        category.setCid(cid);
        category.setCname(cname);
        category.setCdesc(cdesc);

        //调用业务层
        CategoryService categoryService = new CategoryServiceImpl();
        categoryService.update(category);

        //返回列表页面
        response.sendRedirect(request.getContextPath()+"CategoryServlet?method=findAll");

    }

    /**
     * 分类信息修改
     * @param request
     * @param response
     */
    private void edit(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        //获取参数
        Integer cid = Integer.parseInt(request.getParameter("cid"));
        //调用业务层
        CategoryService categoryService = new CategoryServiceImpl();
        Category category = categoryService.findOne(cid);
        //返回页面
        request.setAttribute("category",category);
        request.getRequestDispatcher(request.getContextPath()+"/admin/category_update.jsp").forward(request,response);

    }

    /**
     *后台分类信息新增保存
     * @param request
     * @param response
     */
    private void save(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //接收数据
        //封装数据
        //传业务层处理数据
        //页面跳转
        String cname = request.getParameter("cname");
        String cdes  = request.getParameter("cdesc");
        Category category = new Category();
        category.setCname(cname);
        category.setCdesc(cdes);
        CategoryService categoryService = new CategoryServiceImpl();
        categoryService.save(category);

        response.sendRedirect(request.getContextPath()+"CategoryServlet?method=findAll");

    }

    /**
     *后台分类管理保存分类信息
     * @param request
     * @param response
     */
    private void saveUI(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("/admin/category_add.jsp").forward(request,response);
        //接受参数
        //封装数据
        //调用业务层处理
        //页面跳转

    }


    /**
     * 后台分类管理查询所有的分类方法
     * @param request
     * @param response
     */
    private void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收参数
        //封装数据
        //调用业务层处理数据
        //页面跳转
        CategoryService categoryService = new CategoryServiceImpl();
        List<Category> list = categoryService.findAll();

        request.setAttribute("list",list);
        request.getRequestDispatcher("/admin/category_list.jsp").forward(request,response);

    }
}
